package gdcp.demo_4.cn;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

public class demo_4_Activity extends AppCompatActivity implements IOnDelListener {
    private long lastPressTime = 0;
    private ArrayList<Student> studentArrayList = new ArrayList<>();
    private ArrayList<Student> searchResultList = new ArrayList<>();
    private MyAdapter adapter;
    private Button btnAddStu;
    private ListView stuListView;
    private EditText edtKeyword;
    private Button btnSearch;

    private boolean isResultList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.demo_4_layoout);

        initStuArrayList();

        initAdapter();

        initListView();

        initBtnAdd();

        initSearchView();
    }


    /**
     * 初始化搜索模块
     */
    private void initSearchView() {
        edtKeyword = findViewById(R.id.edt_keyword);
        btnSearch = findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String keyword = edtKeyword.getText().toString().trim();
                //使用学生的列表和关键字 得到搜索结果列表
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });
        edtKeyword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyword = edtKeyword.getText().toString().trim();
                //使用学生列表和关键字 得到搜索结果列表
                search(studentArrayList, keyword);
                adapter.changeData(searchResultList);
                isResultList = true;
            }
        });
    }

    /**
     * 使用学生列表和关键字 得到搜索结果列表
     *
     * @param studentArrayList
     * @param keyword
     */
    private void search(ArrayList<Student> studentArrayList, String keyword) {
        searchResultList.clear();
        for (int i = 0; i < studentArrayList.size(); i++) {
            Student s = studentArrayList.get(i);
            //如果学生姓名包含了关键字 这个学生就加入到结果列表
            if (s.getName().contains(keyword)) {
                searchResultList.add(s);
            }
            //如果学生年龄等于关键字 这个学生就加入到结果列表
            else if (String.valueOf(s.getAge()).equals(keyword)) {
                searchResultList.add(s);
            }
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.e("demo_4_Activity", "requestCode = " + requestCode + "resultCode = " + resultCode);

        if (data == null) {
            return;
        }

        if (resultCode == 2001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.img1);

            Student s = new Student("10086", imgId, name, age);
            studentArrayList.add(s);
            adapter.notifyDataSetChanged();
        } else if (resultCode == 3001) {
            String name = data.getStringExtra("NAME");
            int age = data.getIntExtra("AGE", 0);
            int imgId = data.getIntExtra("IMG", R.drawable.img6);
            String no = data.getStringExtra("NO");

            for (int i = 0; i < studentArrayList.size(); i++) {
                Student student = studentArrayList.get(i);

                if (student.getNo().equals(no)) {
                    student.setName(name);
                    student.setAge(age);
                    student.setImgId(imgId);
                }
            }
            String keyword = edtKeyword.getText().toString().trim();
            //使用学生列表和关键字 得到搜索结果列表
            search(studentArrayList, keyword);
            adapter.changeData(searchResultList);
            isResultList = true;
        }
    }


    @Override
    public void onBackPressed() {
        long currentPressTime = System.currentTimeMillis();
        if (currentPressTime - lastPressTime > 2000) {
            lastPressTime = currentPressTime;
            Toast.makeText(demo_4_Activity.this, "再按一次退出", Toast.LENGTH_LONG).show();
        } else {
            super.onBackPressed();
        }
    }


    @Override
    public void del(int i) {
        Log.e("demo_4_Activity", "del" + i);
        studentArrayList.remove(i);
        adapter.notifyDataSetChanged();
    }


    private void initStuArrayList() {
        Student s1 = new Student("10081", R.drawable.img1, "牛顿", 30);
        Student s2 = new Student("10082", R.drawable.img2, "科加斯", 28);
        Student s3 = new Student("10083", R.drawable.img3, "科比", 36);
        Student s4 = new Student("10084", R.drawable.img4, "姚明", 37);
        Student s5 = new Student("10085", R.drawable.img5, "詹姆斯", 35);
        studentArrayList.add(s1);
        studentArrayList.add(s2);
        studentArrayList.add(s3);
        studentArrayList.add(s4);
        studentArrayList.add(s5);
    }

    /**
     * 初始化学生列表
     */
    private void initListView() {
        stuListView = findViewById(R.id.Stu_listview);
        stuListView.setAdapter(adapter);

        stuListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Student s = studentArrayList.get(i);
                if (isResultList) {
                    s = searchResultList.get(i);
                }
                Toast.makeText(demo_4_Activity.this, "姓名" + s.getName() + "年龄" + s.getAge(), Toast.LENGTH_SHORT).show();

                Intent intent = new Intent();
                intent.setClass(demo_4_Activity.this, EditActivity.class);
                intent.putExtra("NAME", s.getName());
                intent.putExtra("AGE", s.getAge());
                intent.putExtra("IMGID", s.getImgid());
                intent.putExtra("NO", s.getNo());
                startActivityForResult(intent, 1002);

            }
        });
    }


    /**
     * 初始化自定义适配器
     */
    private void initAdapter() {
        adapter = new MyAdapter(demo_4_Activity.this, studentArrayList, demo_4_Activity.this);
    }


    /**
     * 初始化添加按钮和点击事件
     */
    private void initBtnAdd() {
        btnAddStu = findViewById(R.id.btn_addStu);
        btnAddStu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(demo_4_Activity.this, Addactivity.class);
                startActivityForResult(intent, 1001);

            }
        });
    }
}
